package com.example.arrange;

import com.example.util.CommonUtils;

import java.util.concurrent.CompletableFuture;

public class ThenCombineDemo {

    public static void main(String[] args) {
        CommonUtils.printThreadLog("main start");

        CompletableFuture<String> newsFuture = CompletableFuture.supplyAsync(() -> {
            CommonUtils.printThreadLog("读取文件：news.txt");
            return CommonUtils.readFile("news.txt");
        });

        CompletableFuture<String[]> filterWordsFuture = CompletableFuture.supplyAsync(() -> {
            CommonUtils.printThreadLog("读取文件：filter_words.txt");
            String filterWords = CommonUtils.readFile("filter_words.txt");
            return filterWords.split(",");
        });

        CompletableFuture<String> replacedNewsFuture = newsFuture.thenCombine(filterWordsFuture, (news, filterWords) -> {
            CommonUtils.printThreadLog("替换敏感词");
            for (String filterWord : filterWords) {
                if (news.contains(filterWord)) {
                    news = news.replaceAll(filterWord, "***");
                }
            }
            return news;
        });

        CommonUtils.printThreadLog("main continue");

        String news = replacedNewsFuture.join();
        System.out.println(news);

        CommonUtils.printThreadLog("main end");
    }

}
